Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 799 Bytes

File metadata and controls

38 lines (31 loc) · 799 Bytes

49. Group Anagrams

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"] Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] 

Note:

  • All inputs will be in lowercase.
  • The order of your output does not matter.

Solutions (Rust)

1. HashMap

use std::collections::HashMap;implSolution{pubfngroup_anagrams(strs:Vec<String>) -> Vec<Vec<String>>{letmut anagrams = HashMap::new();for s in strs {letmut cnt = [0;26]; s.bytes().for_each(|c| cnt[(c - b'a')asusize] += 1); anagrams.entry(cnt).or_insert(Vec::new()).push(s);} anagrams.values().cloned().collect()}}
close